package com.lepu.app.fun.hospital.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.Setting;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.hospital.bean.Consult;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.ListViewForScrollview;

public class ConsultAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	private ArrayList<Consult> mArrayList = null;

	public ConsultAdapter(Context context, ArrayList<Consult> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.hospital_consult_question_item, null);

			holder = new ViewHolder();

			RelativeLayout rightLayout = (RelativeLayout) convertView
					.findViewById(R.id.rightLayout);
			TextView consult_question_item_text = (TextView) convertView
					.findViewById(R.id.consult_question_item_text);
			CircularImageView consult_question_item_icon = (CircularImageView) convertView
					.findViewById(R.id.consult_question_item_icon);

			LinearLayout leftLayout = (LinearLayout) convertView
					.findViewById(R.id.leftLayout);
			TextView descTextView = (TextView) convertView
					.findViewById(R.id.descTextView);
			ListViewForScrollview dataListView = (ListViewForScrollview) convertView.findViewById(R.id.dataListView);

			holder.rightLayout = rightLayout;
			holder.consult_question_item_text = consult_question_item_text;
			holder.consult_question_item_icon = consult_question_item_icon;

			holder.leftLayout = leftLayout;
			holder.dataListView = dataListView;
			holder.descTextView = descTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Consult item = mArrayList.get(position);

		if (item.type == Consult.TYPE_QUESTION) {
			holder.leftLayout.setVisibility(View.GONE);
			holder.rightLayout.setVisibility(View.VISIBLE);

			if(MyApplication.getInstance().checkIsLogin())
			{
				LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
//				String userPath = SdLocal.getUserPath(mContext, loginInfo.UserID);
//				if(new File(userPath).exists())
//				{
//					holder.consult_question_item_icon.setImageBitmap(ImageUtilBase.getBitmap(userPath));
//				}
				String imageUrl = Setting.getImageUrl(loginInfo.Avatar);
				UniversalImageLoadTool.disPlay(imageUrl, holder.consult_question_item_icon, R.drawable.default_icon_small);
			}else{
				holder.consult_question_item_icon.setImageResource(R.drawable.icon_head_nologin);
			}
			
			holder.consult_question_item_text.setText(item.head);

		} else {
			holder.leftLayout.setVisibility(View.VISIBLE);
			holder.rightLayout.setVisibility(View.GONE);
			
			holder.descTextView.setText(item.desc);
			holder.dataListView.setAdapter(new AdapterHospitalQuestionList(mContext, item.dataList));
		}

		return convertView;
	}
	
	static class ViewHolder {
		RelativeLayout rightLayout;
		TextView consult_question_item_text;
		CircularImageView consult_question_item_icon;

		LinearLayout leftLayout;
		TextView descTextView;
		ListViewForScrollview dataListView;
	}

}
